from people_flow_generator import PeopleFlowGenerator
from security_simulation import SecuritySimulation
from datetime import datetime


class SubwaySecurityModel:
    """地铁安检模型，整合人流生成和安检模拟"""
    
    def __init__(self, config_path: str):
        self.config_path = config_path
        self.flow_generator = PeopleFlowGenerator(config_path)
        self.security_simulator = SecuritySimulation(config_path)
    
    def run_morning_simulation(self, base_date: datetime = None) -> dict:
        """运行上午的模拟"""
        print("开始生成上午人流...")
        morning_people = self.flow_generator.generate_morning_flow(base_date)
        print(f"上午共生成 {len(morning_people)} 人")
        
        print("开始模拟上午安检过程...")
        self.security_simulator = SecuritySimulation(self.config_path)  # 重置模拟器
        self.security_simulator.simulate(morning_people, 'morning')
        
        return self.security_simulator.get_results('morning')
    
    def run_afternoon_simulation(self, base_date: datetime = None) -> dict:
        """运行下午的模拟"""
        print("开始生成下午人流...")
        afternoon_people = self.flow_generator.generate_afternoon_flow(base_date)
        print(f"下午共生成 {afternoon_people} 人")
        
        print("开始模拟下午安检过程...")
        self.security_simulator = SecuritySimulation(self.config_path)  # 重置模拟器
        self.security_simulator.simulate(afternoon_people, 'afternoon')
        
        return self.security_simulator.get_results('afternoon')
    
    def run_full_simulation(self, base_date: datetime = None) -> dict:
        """运行完整的模拟（上午和下午）"""
        if base_date is None:
            base_date = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
        
        # 运行上午模拟
        morning_results = self.run_morning_simulation(base_date)
        
        # 运行下午模拟
        afternoon_results = self.run_afternoon_simulation(base_date)
        
        return {
            'morning': morning_results,
            'afternoon': afternoon_results
        }
    
    def print_results(self, results: dict):
        """打印模拟结果"""
        print("\n===== 模拟结果 =====")
        print("\n参数说明：")
        print("总人数：模拟时间段内从地铁口出来的总人流量")
        print("平均总时长：从出地铁口到通过安检口到达E1口的平均时间")
        print("最长总时长：从出地铁口到通过安检口到达E1口的最长时间")
        print("最短总时长：从出地铁口到通过安检口到达E1口的最短时间")
        print("平均等待时间：在安检口前排队等待的平均时间")
        print("最长等待时间：在安检口前排队等待的最长时间")
        print("安检不通过人数：安检未通过需要重新安检的人数")
        
        for period, data in results.items():
            if period == 'morning':
                print("\n----- 上午时段 -----")
            else:
                print("\n----- 下午时段 -----")
            
            if not data:
                print("无数据")
                continue
                
            print(f"总人数: {data['total_people']}")
            print(f"平均总时长: {data['avg_total_time']:.2f} 分钟")
            print(f"最长总时长: {data['max_total_time']:.2f} 分钟")
            print(f"最短总时长: {data['min_total_time']:.2f} 分钟")
            print(f"平均等待时间: {data['avg_wait_time']:.2f} 分钟")
            print(f"最长等待时间: {data['max_wait_time']:.2f} 分钟")
            print(f"安检不通过人数: {data['failed_people']}")